package icu.javayanglei.bilibili.community.service.impl;

import cn.hutool.core.lang.Console;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import icu.javayanglei.bilibili.community.service.BulletScreenService;
import icu.javayanglei.bilibili.community.service.VideoService;
import icu.javayanglei.bilibili.domain.Video;
import icu.javayanglei.bilibili.mapper.VideoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yang
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video>
        implements VideoService {
    @Autowired
    private BulletScreenService service;

    @Autowired
    private VideoService videoService;

    @Override
    public void saveBV(String uid) {
        final List<String> list = service.BVList("177925547");

        final List<Video> collect = list.stream().map(e -> Video.builder()
                .bvid(e)
                .build()).collect(Collectors.toList());

        videoService.saveBatch(collect);
    }

    @Override
    public void saveVideoByBV() {
        final List<Video> collect = this.list().stream()
                // 没有 cid 就去查找
                .filter(e -> StrUtil.isBlank(e.getCid()))
                .map(e -> {
                    final Video video = service.queryByBV(e.getBvid());
                    video.setId(e.getId());
                    Console.log("更新后的 video ",video);
                    return video;
                }).collect(Collectors.toList());

        this.saveOrUpdateBatch(collect);
    }
}




